package com.youmingxing.activity;

import android.os.Bundle;

import com.youmingxing.R;
import com.youmingxing.adpter.CommentsAdapter;
import com.youmingxing.bean.Comment;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chihiro(xuchang)
 * @since 2015-10-11
 */

public class CommentListActivity extends BaseActivity {

    RefreshListView listView;
    List<Comment> commentList;
    CommentsAdapter adapter;
    int contentId;
    int mark = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        commentList = new ArrayList<>();
        contentId = getIntent().getExtras().getInt("id");
        setTopbarName("热门评论");
        initView();
    }

    private void initView() {
        listView = (RefreshListView) findViewById(R.id.list_view);
        listView.setOnLoadMoreListener(new RefreshListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                AllStarApi.getGetHotComments(contentId, mark, jsonHandler);
            }
        });
        adapter = new CommentsAdapter(null, this);
        listView.setAdapter(adapter);
        AllStarApi.getGetHotComments(contentId, mark, jsonHandler);

    }

    BaseJsonHandler jsonHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            JSONArray data = response.optJSONArray("data");
            if (data.length() == 0) {
                listView.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                Comment comment = new Comment(data.optJSONObject(i));
                commentList.add(comment);
            }
            adapter.setData(commentList);
            mark++;
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            listView.endLoadMore();

        }
    };
}
